package com.tmt.datatable.editor.impl;

import com.tmt.datatable.editor.IFieldMappingHandler;
import com.tmt.datatable.editor.IFieldMappingHandlerProxy;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ThreeManTeam on 2017/6/6.
 */
public class DefaultFieldMappingHandlerProxy implements IFieldMappingHandlerProxy {

    private List<IFieldMappingHandler> handlers = new ArrayList<>();

    @Override
    public boolean support(Class clazz, String propName) {
        return handlers.stream().anyMatch(x->x.support(clazz, propName));
    }

    @Override
    public <T> String getValidFieldName(Class<T> clazz, String propName, Object value, String action) throws Exception {
        String realFieldName = propName;
        for (IFieldMappingHandler handler : handlers) {
            if (handler.support(clazz, propName)) {
                realFieldName = handler.getValidFieldName(clazz, propName, value, action);
                break;
            }
        }
        return realFieldName;
    }

    @Override
    public void registerHandler(IFieldMappingHandler handler) {
        handlers.add(handler);
    }

}
